package com.qf.fmall.service.impl;

import com.qf.fmall.dao.CategoryMapper;
import com.qf.fmall.dao.IndexImgMapper;
import com.qf.fmall.dao.ProductMapper;
import com.qf.fmall.entity.CategoryVo;
import com.qf.fmall.entity.IndexImg;
import com.qf.fmall.entity.Product;
import com.qf.fmall.service.CategoryService;
import com.qf.fmall.vo.ResStatus;
import com.qf.fmall.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;

@Service("categoryService")
public class CategoryServiceImpl implements CategoryService {
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private CategoryMapper categoryMapper;
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private IndexImgMapper indexImgMapper;

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private ProductMapper productMapper;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<CategoryVo> getCategoryName() {
        List<CategoryVo> categoryVoList = categoryMapper.getCategoryName();
        if (Objects.isNull(categoryVoList)) {
            return null;
        }
        return categoryVoList;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<IndexImg> getImgList() {
        Example example = new Example(IndexImg.class);
        example.createCriteria()
                .andEqualTo("status", 1);
        List<IndexImg> list = indexImgMapper.selectByExample(example);
        return list;
    }

    /**
     * 分类商品推荐展示
     *
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVo categoryrecommends() {
        List<CategoryVo> list = categoryMapper.categoryrecommends();
        ResultVo resultVo = new ResultVo();
        if ((list.size() == 0)) {
            resultVo.setCode(ResStatus.NO);
            resultVo.setMsg("查询失败");
        } else {
            resultVo.setCode(ResStatus.OK);
            resultVo.setMsg("查询成功");
            resultVo.setData(list);
        }
        return resultVo;
    }


}
